#include "../../muduo_source/server.hpp"

class echoServer
{
private:
    TcpServer _server;

private:
    void OnMessage(const PtrConnection& conn,Buffer* buff)
    {
        //打印收到的消息
        //DEBUG_LOG("我收到了消息：%s",buff->GetReadPos());
        conn->Send(buff->GetReadPos(),buff->GetReadableSpace()); 

        buff->MoveReadOffset(buff->GetReadableSpace());//读指针此时移动到与写指针相同位置
        //std::string str = "服务器收到数据啦，hello";
        conn->Shutdown();//?
    }

    void OnConnected(const PtrConnection& conn)
    {
        DEBUG_LOG("NEW CONNECTION: %p",conn.get());
    }

    void OnClosed(const PtrConnection& conn)
    {
        DEBUG_LOG("CLOSE CONNECTION : %p",conn.get()); //不一定是客户端主动退出，而是客户端非活跃
    }


public:
    echoServer(int port)
        :_server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        
        _server.SetConnectedCallback(std::bind(&echoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&echoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&echoServer::OnClosed,this,std::placeholders::_1));

    }
    void Start()
    {
        _server.Start();
    }
};

